home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Entertainment / MacMud / Sockets / testspin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-30  |  2.0 KB  |  112 lines  |  [TEXT/MPS ]

  1. #include <Events.h>
  2. #include <memory.h>
  3. #include <types.h>
  4. #include <OSUtils.h> /* for SysBeep */
  5.  
  6. #include <stdio.h>
  7.  
  8. #include <sys/types.h>
  9. #include <sys/time.h>
  10. #include <sys/errno.h>
  11. #include <sys/socket.h>
  12. #include <sys/ioctl.h>
  13. #include <netinet/in.h>
  14. #include <sys/uio.h>
  15.  
  16. int spins = 0;
  17. chasemouse()
  18. {
  19.     spins++;
  20. }
  21.  
  22. main()
  23. {
  24.     int s;
  25.     struct sockaddr_in her;
  26.     int herlen = sizeof(her);
  27.     int bytes;
  28.     char line[1000];
  29.             
  30.     /* make our socket */
  31.     s = s_socket(AF_INET, SOCK_STREAM, 0);
  32.     if (s < 0)
  33.     {
  34.         perror("socket");    
  35.         exit(1);
  36.     }
  37.     
  38.     /* define our spin routine */
  39.     if (s_spinroutine(chasemouse) < 0)
  40.     {
  41.         perror("spinroutine");    
  42.         exit(1);
  43.     }
  44.     
  45.     /* connect to the server */
  46.     her.sin_family = AF_INET;
  47.     her.sin_addr.s_addr = 0x0d000ce8 /* 0x8064660a */;
  48.     her.sin_port = htons(25); /* SMTP */
  49.     if (s_connect(s,(caddr_t)&her,sizeof(her)) < 0)
  50.     {
  51.         if (errno != EINPROGRESS)
  52.         {
  53.             perror("connect");    
  54.             exit(1);
  55.         }
  56.     }
  57.     dprintf("%d spins after connect\n",spins);
  58.  
  59.     /* wait for the server's greeting */
  60.     bytes = s_read(s,line,sizeof(line)-2);
  61.     if (bytes < 0)
  62.     {
  63.         perror("read");
  64.         exit(1);
  65.     }
  66.     dprintf("%d spins after read\n",spins);
  67.     dprintf("read got %d bytes\n",bytes);
  68.     line[bytes] = '\0';
  69.     dprintf("'%s'\n",line);
  70.     
  71.     /* send our own introduction */
  72.     strcpy(line,"HELO milligan.utcs.utoronto.ca\015\012");
  73.     bytes = s_write(s,line,strlen(line));
  74.     if (bytes <= 0)
  75.     {
  76.         perror("write");
  77.         exit(1);
  78.     }
  79.     dprintf("%d spins after write\n",spins);
  80.         
  81.     /* wait for him to decide he likes us and */
  82.     /* read his approval message (or disapproval...) */
  83.     bytes = s_read(s,line,sizeof(line)-2);
  84.     if (bytes < 0)
  85.     {
  86.         perror("read");
  87.         exit(1);
  88.     }
  89.     dprintf("%d spins after read\n",spins);
  90.     dprintf("read got %d bytes\n",bytes);
  91.     line[bytes] = '\0';
  92.     dprintf("'%s'\n",line);
  93.     
  94.     /* now lets be rude and quit the conversation */
  95.     strcpy(line,"QUIT\015\012");
  96.     bytes = s_write(s,line,strlen(line));
  97.     if (bytes <= 0 && errno != EINPROGRESS)
  98.     {
  99.         perror("write");
  100.         exit(1);
  101.     }
  102.     dprintf("%d spins after write\n",spins);
  103.  
  104.     /* clean up */
  105.     if (s_close(s) < 0)
  106.     {
  107.         perror("close(s)");
  108.         exit(1);
  109.     }
  110. }
  111.  
  112.